/*	This program produces Figure D.1 */

***** Set directories 
local dir_clean 	"~/Dropbox/Retirement gaming/clean"
local dir_output 	"~/Dropbox/Retirement gaming/output/dataverse"


use "`dir_clean'/mainsample_age50analysis.dta", clear

local listcontrols = " i.t i.ciiu2_1stobs i.ndep_cat_1stobs i.ndep_cat i.year#i.i.ciiu2_1stobs i.year#i.ndep_cat_1stobs "  


** SELF EMPLOYED EARNINGS
preserve
keep if self_empl==1 
*Normalize wages
sum W if age_centered==-1
local meanw=r(mean)
g Wz=W/`meanw'
* Age dummies
tab age_centered, gen(age_centered_dums)
drop age_centered_dums5 // drop year before ref age, so everything becomes relative to that
* Regression
reghdfe Wz age_centered_dums*, absorb(`listcontrols') vce(cluster i)
* Sample size
global n=e(N)
global N=e(N_clust1)
* Coefficients and sd
for any beta sd: gen X = .
forvalues d = 1(1)13 {
	capture qui replace beta = _b[age_centered_dums`d'] if age_centered==`d'-6
	capture qui replace sd   = _se[age_centered_dums`d'] if age_centered==`d'-6
}
replace beta=0 if age_centered==-1
replace sd=0 if age_centered==-1
* Plot the results
collapse beta* sd*, by(age_centered)
gen sd_top = beta + 1.96*sd	
gen sd_bot = beta - 1.96*sd
gen sd_top10 = beta + 1.645*sd	
gen sd_bot10 = beta - 1.645*sd
twoway rcap sd_top sd_bot age_centered, ///
	cmissing(n) lwidth(thin) lcolor(navy) lpattern(solid) xsc(r(-5 7)) xlabel(-5(1)7)  xline(-1) xline(3.5, lp(dash) ) ysc(r(-0.03 .09)) ylabel(-0.03(0.03).09,grid)  ///
	|| rcap sd_top10 sd_bot10 age_centered, cmissing(n) lwidth(thin) lcolor(navy) lpattern(solid)  xsc(r(-5 7)) ///
	|| scatter beta age_centered, mcolor(navy) scheme(s1color) lcolor(navy) lpattern(solid) yline(0, lcolor(gs2)) ///
	title("Reported earnings, relative to age-49 average") note("N=${n}, Individuals=${N}") ///
	legend(off) ytitle("Estimated Coefficients") xtitle("Years Relative to Age 50")   
graph export "`dir_output'/figureD1a.png",  replace 	
restore


*** EMPLOYEES SMALL FIRMS (LESS THAN 10 WORKERS) EARNINGS
preserve
keep if empl==1  &  small_1stobs==1  
*Normalize wages
sum W if age_centered==-1
local meanw=r(mean)
g Wz=W/`meanw'
* Age dummies
tab age_centered, gen(age_centered_dums)
drop age_centered_dums5
* Regression
reghdfe Wz age_centered_dums* , absorb(`listcontrols') vce(cluster i )
* Sample size
global n=e(N)
global N=e(N_clust1)
* Coefficients and sd
for any beta sd: gen X = .
forvalues d = 1(1)13 {
	capture qui replace beta = _b[age_centered_dums`d'] if age_centered==`d'-6
	capture qui replace sd   = _se[age_centered_dums`d'] if age_centered==`d'-6
}
replace beta=0 if age_centered==-1
replace sd=0 if age_centered==-1
* Plot the results
collapse beta* sd*, by(age_centered)
gen sd_top = beta + 1.96*sd	
gen sd_bot = beta - 1.96*sd
gen sd_top10 = beta + 1.645*sd	
gen sd_bot10 = beta - 1.645*sd
twoway rcap sd_top sd_bot age_centered, ///
	cmissing(n) lwidth(thin) lcolor(navy) lpattern(solid) xsc(r(-5 7)) xlabel(-5(1)7)  xline(-1) xline(3.5, lp(dash) ) ysc(r(-0.03 .09)) ylabel(-0.03(0.03).09,grid)  ///
	|| rcap sd_top10 sd_bot10 age_centered, cmissing(n) lwidth(thin) lcolor(navy) lpattern(solid) xsc(r(-5 7)) ///
	|| scatter beta age_centered, mcolor(navy) scheme(s1color) lcolor(navy) lpattern(solid) yline(0, lcolor(gs2)) ///
	title("Reported earnings, relative to age-49 average")  note("N=${n}, Individuals=${N}") ///
	legend(off) ytitle("Estimated Coefficients") xtitle("Years Relative to Age 50")   
graph export "`dir_output'/figureD1b.png",  replace 	
restore
		

*** EMPLOYED -- LARGE SIZED FIRMS (MORE THAN 10 WORKERS) EARNINGS
preserve
keep if empl==1 & large_1stobs==1 
*Normalize wages
sum W if age_centered==-1
local meanw=r(mean)
g Wz=W/`meanw'
* Age dummies
tab age_centered, gen(age_centered_dums)
drop age_centered_dums5 // drop year before ref age, so everything becomes relative to that
* Regression
reghdfe Wz  age_centered_dums*, absorb(`listcontrols') vce(cluster i )
* Sample size
global n=e(N)
global N=e(N_clust1)
* Coefficients and sd
for any beta sd: gen X = .
forvalues d = 1(1)13 {
	capture qui replace beta = _b[age_centered_dums`d'] if age_centered==`d'-6
	capture qui replace sd   = _se[age_centered_dums`d'] if age_centered==`d'-6
}
replace beta=0 if age_centered==-1
replace sd=0 if age_centered==-1
* Plot the results
collapse beta* sd*, by(age_centered)
gen sd_top = beta + 1.96*sd	
gen sd_bot = beta - 1.96*sd
gen sd_top10 = beta + 1.645*sd	
gen sd_bot10 = beta - 1.645*sd
twoway rcap sd_top sd_bot age_centered, ///
	cmissing(n) lwidth(thin) lcolor(navy) lpattern(solid) xsc(r(-5 7)) xlabel(-5(1)7)  xline(-1) xline(3.5, lp(dash) ) ysc(r(-0.03 .09)) ylabel(-0.03(0.03).09,grid) ///
	|| rcap sd_top10 sd_bot10 age_centered, cmissing(n) lwidth(thin) lcolor(navy) lpattern(solid) xsc(r(-5 7)) ///
	|| scatter beta age_centered, mcolor(navy) scheme(s1color) lcolor(navy) lpattern(solid) yline(0, lcolor(gs2)) ///
	title("Reported earnings, relative to age-49 average") note("N=${n}, Individuals=${N}") ///
	legend(off) ytitle("Estimated Coefficients") xtitle("Years Relative to Age 50")   
graph export "`dir_output'/figureD1c.png",  replace 	
restore

clear all
exit
